package br.com.gecandango.portlets.cardapio;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import br.com.gecandango.beans.Cardapio;
import br.com.gecandango.beans.Prato;
import br.com.gecandango.beans.Refeicao;

public class CardapioPortlet extends CardapioAction {

	static final Logger logger = Logger
	.getLogger(CardapioPortlet.class);
	
	public void processAction(ActionMapping mapping, ActionForm form,
			PortletConfig config, ActionRequest req, ActionResponse res)
			throws Exception {
		String acao = req.getParameter("acao");
		logger.info("acao: "+acao);
		if (acao.equals("add")){
			setForward(req, "portlet.ext.cardapio.edit");
		} else if (acao.equals("add_refeicao")){
			setForward(req, "portlet.ext.cardapio.edit.refeicao");
		} else if (acao.equals("add_prato")){
			setForward(req, "portlet.ext.cardapio.edit.prato");
			
			
		} else if (acao.equals("add_pr")){
			req.setAttribute("refeicao", salvarRefeicao(req));
			req.setAttribute("pratos", getPratoService().list());
			setForward(req, "portlet.ext.cardapio.list.pr");
		} else if (acao.equals("add_rc")){
			req.setAttribute("cardapio", salvarCardapio(req));
			req.setAttribute("refeicoes", getRefeicaoService().listRefeicoes());
			setForward(req, "portlet.ext.cardapio.list.rc");
		} else if (acao.equals("add_prato")){
			setForward(req, "portlet.ext.cardapio.edit.prato");
				
			
			
		} else if (acao.equals("list")){
			req.setAttribute("cardapios", getCardapioService().list());
			setForward(req, "portlet.ext.cardapio.list");
		} else if (acao.equals("list_refeicoes")){
			req.setAttribute("refeicoes", getRefeicaoService().listRefeicoes());
			setForward(req, "portlet.ext.cardapio.list.refeicoes");
		} else if (acao.equals("list_pratos")){
			req.setAttribute("pratos", getPratoService().list());
			setForward(req, "portlet.ext.cardapio.list.pratos");
		
		
		} else if (acao.equals("edit")){
			logger.info("edit");
			req.setAttribute("cardapio", getCardapioService().find(Long.parseLong(req
					.getParameter("id_cardapio"))));
			setForward(req, "portlet.ext.cardapio.edit");
		} else if (acao.equals("edit_refeicao")){
			req.setAttribute("refeicao", getRefeicaoService().findRefeicao(Long.parseLong(req
					.getParameter("id_refeicao"))));
			setForward(req, "portlet.ext.cardapio.edit.refeicao");
		} else if (acao.equals("edit_prato")){
			req.setAttribute("prato", getPratoService().find(Long.parseLong(req
					.getParameter("id_prato"))));
			setForward(req, "portlet.ext.cardapio.edit.prato");
		
		
		} else if (acao.equals("delete")){
			getCardapioService().delete(getCardapioService().find(Long.parseLong(req
					.getParameter("id_cardapio"))));
			req.setAttribute("cardapios", getCardapioService().list());
			setForward(req, "portlet.ext.cardapio.list");
		} else if (acao.equals("delete_refeicao")){
			getRefeicaoService().delete(getRefeicaoService().findRefeicao(Long.parseLong(req
					.getParameter("id_refeicao"))));
			req.setAttribute("refeicoes", getRefeicaoService().listRefeicoes());
			setForward(req, "portlet.ext.cardapio.list.refeicoes");
		} else if (acao.equals("delete_prato")){
			getPratoService().delete(getPratoService().find(Long.parseLong(req
					.getParameter("id_prato"))));
			req.setAttribute("pratos", getPratoService().list());
			setForward(req, "portlet.ext.cardapio.list.pratos");
		
		
		} else if (acao.equals("salvar")){
			req.setAttribute("cardapio", salvarCardapio(req));
			req.setAttribute("cardapios", getCardapioService().list());
			setForward(req, "portlet.ext.cardapio.list");
		} else if (acao.equals("salvar_refeicao")){
			req.setAttribute("refeicao", salvarRefeicao(req));
			req.setAttribute("refeicoes", getRefeicaoService().listRefeicoes());
			setForward(req, "portlet.ext.cardapio.list.refeicoes");
		} else if (acao.equals("salvar_prato")){
			req.setAttribute("prato", salvarPrato(req));
			req.setAttribute("pratos", getPratoService().list());
			setForward(req, "portlet.ext.cardapio.list.pratos");
		} else if (acao.equals("salvar_pr")){
			req.setAttribute("refeicao", salvarRefeicao(req));
			setForward(req, "portlet.ext.cardapio.edit.refeicao");
		} else if (acao.equals("salvar_rc")){
			req.setAttribute("cardapio", salvarCardapio(req));
			setForward(req, "portlet.ext.cardapio.edit");
			
			
		} else if (acao.equals("delete_pr")){
			req.setAttribute("refeicao", salvarRefeicao(req));
			setForward(req, "portlet.ext.cardapio.edit.refeicao");
		} else if (acao.equals("delete_rc")){
			req.setAttribute("cardapio", salvarCardapio(req));
			setForward(req, "portlet.ext.cardapio.edit");
			
		} else {
			setForward(req, "portlet.ext.cardapio.view");
		}
	}
	public Cardapio salvarCardapio(ActionRequest request){
		String id = (String) request.getParameter("id_cardapio");
		String nome = (String) request.getParameter("nome");
		String descricao = (String) request.getParameter("descricao");
		String endereco = (String) request.getParameter("endereco");
		String telefone = (String) request.getParameter("telefone");
		String email = (String) request.getParameter("email");
		String acao = (String) request.getParameter("acao");
		
		
		Cardapio cardapio = new Cardapio();
		
		if(id!= null){
			try {
				cardapio = getCardapioService().find(Long.parseLong(id));
				if(acao.equals("delete_rc")){
					Refeicao refeicao = getRefeicaoService().findRefeicao(Long.parseLong(request
							.getParameter("id_refeicao")));
					cardapio.getRefeicoes().remove(refeicao);
					return getCardapioService().save(cardapio);
				}
				if(acao.equals("salvar_rc")){
					Refeicao refeicao = getRefeicaoService().findRefeicao(Long.parseLong(request
							.getParameter("id_refeicao")));
					cardapio.getRefeicoes().add(refeicao);
					return getCardapioService().save(cardapio);
				}
			} catch (Exception e) {
			}
		}
		cardapio.setNome(nome);
		
		cardapio.setDescricao(descricao);
		
		try {
			return getCardapioService().save(cardapio);
		} catch (Exception e) {
			return null;
		}		
	}
	
	public Refeicao salvarRefeicao(ActionRequest request){
		String id = (String) request.getParameter("id_refeicao");
		String nome = (String) request.getParameter("nome");
		String descricao = (String) request.getParameter("descricao");
		String endereco = (String) request.getParameter("endereco");
		String telefone = (String) request.getParameter("telefone");
		String email = (String) request.getParameter("email");
		
		String acao = (String) request.getParameter("acao");
		
		Refeicao refeicao = new Refeicao();
		
		if(id!= null){
			try {
				refeicao = getRefeicaoService().findRefeicao(Long.parseLong(id));
				if(acao.equals("delete_pr")){
					Prato prato = getPratoService().find(Long.parseLong(request
							.getParameter("id_prato")));
					refeicao.getPratos().remove(prato);
					return getRefeicaoService().save(refeicao);
				}
				if(acao.equals("salvar_pr")){
					Prato prato = getPratoService().find(Long.parseLong(request
							.getParameter("id_prato")));
					refeicao.getPratos().add(prato);
					return getRefeicaoService().save(refeicao);
				}
			} catch (Exception e) {
				logger.info("Exception: "+e);
			}
		}
		refeicao.setNome(nome);
		
		
		try {
			return getRefeicaoService().save(refeicao);
		} catch (Exception e) {
			return null;
		}		
	}
	
	public Prato salvarPrato(ActionRequest request){
		String id = (String) request.getParameter("id_prato");
		String nome = (String) request.getParameter("nome");
		String descricao = (String) request.getParameter("descricao");
		String endereco = (String) request.getParameter("endereco");
		String telefone = (String) request.getParameter("telefone");
		String email = (String) request.getParameter("email");
		String telefone2 = (String) request.getParameter("telefone2");
		
		
		Prato prato = new Prato();
		
		if(id!= null){
			try {
				prato.setId(Long.parseLong(id));
			} catch (Exception e) {
			}
		}
		prato.setNome(nome);
		
		
		try {
			return getPratoService().save(prato);
		} catch (Exception e) {
			return null;
		}		
	}
}
